var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tanchuDis: false,
   pintuanArray:[],
   companyId:'',
   zheHeight:'',
   pageNo:2,
   courseName:'',
    nowPrice:'',
    oldPrice:'',
    courseIngtroduce:'',
    courseTime:'',
    coursePosition:'',
    id:''
  },
  changeTanchu2:function(event){
    this.setData({
      tanchuDis: !this.data.tanchuDis,

    })
  },
  changeTanchu: function (event) {
    this.setData({
      tanchuDis: !this.data.tanchuDis,

    })
    var that=this
    var id=event.currentTarget.dataset.id
    if(id){
      this.setData({
        id: id
      })
      console.log(event)
      console.log(id)
      wx.request({
        url: app.globalData.baseUrl + 'mechanism/assemble/' + id,
        method: 'get',
        header: {
          'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
        },

        success: function (res) {
          console.log(res)
          var resData = res.data.data
          that.setData({
            courseName: resData.title,
            oldPrice: resData.priceOld,
            nowPrice: resData.priceNow,
            courseIngtroduce: resData.introduce,
            courseTime: resData.schoolTimeDescription,
            coursePosition: resData.position,
            companyId:resData.userId
          })
        }
      })
    }
   
  },
  getPintuan:function(pageNo,start,pageSize){
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'mechanism/assemble',
      method: 'GET',
      data: {
        pageNo: pageNo,
        pageSize: pageSize
      },
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        var resData = res.data.data.data
        if(resData.length==0){
          wx.showToast({
            title: '暂时没有更多数据~~~',
            icon:'none'
          })
        }else{
          for (var i = start; i < start + resData.length; i++) {
            var name = 'pintuanArray[' + i + '].name'
            var newprice = 'pintuanArray[' + i + '].newprice'
            var oldprice = 'pintuanArray[' + i + '].oldprice'
            var description = 'pintuanArray[' + i + '].description'
            var id = 'pintuanArray[' + i + '].id'
            var time = 'pintuanArray[' + i + '].time'
            var position = 'pintuanArray[' + i + '].position'
            var img = 'pintuanArray[' + i + '].img'
            var people = 'pintuanArray[' + i + '].people'
            var num = 'pintuanArray[' + i + '].num'
            var peoplenum = 'pintuanArray[' + i + '].peoplenum'
            
            that.setData({
              [id]: resData[i % pageSize].id,
              [name]: resData[i % pageSize].title,
              [newprice]: resData[i % pageSize].priceNow,
              [oldprice]: resData[i % pageSize].priceOld,
              [description]: resData[i % pageSize].introduce,
              [time]: resData[i % pageSize].schoolTimeDescription,
              [position]: resData[i % pageSize].position,
              [img]: resData[i % pageSize].coverImg,
              [people]: resData[i % pageSize].studentTotal,
              [num]: resData[i % pageSize].studentCurrent,
              [peoplenum]: resData[i % pageSize].studentCurrent
            })
          }
        }
       
        console.log(res)
      },
      fail: function (res) {
        console.log('shibai')
        wx.showToast({
          title: '获取数据失败请刷新',
        })
      }
    })
  },
  join:function(event){
    var that=this
    wx.showModal({
      title: '',
      content: '是否参加拼团',
      success:function(res){
        if(res.confirm){
         if(that.joinAssmble()==1){
           wx.navigateTo({
             url: '/xiaoxi/chat/chat?id=' + that.data.companyId + '&news=' + '您好我想参加贵机构的' + that.data.courseName + '拼团',
           })
         } 
        
        }
      }
    })
  },
  joinAssmble:function(){
  
    var assembleId=this.data.id
    wx.request({
      url: app.globalData.baseUrl+'mechanism/assemble/'+assembleId,
      method:'POST',
      header:{
        'content-type': 'application/x-www-form-urlencoded',
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
          if(res.data.code==0){
            wx.showToast({
              title: '报名成功',
              mask:true,
              duration:3000
            })
            return 1;
          }else if(res.data.code==1102){
            wx.showToast({
              title: '您已参加过该拼团',
              icon:'none',
              mask: true,
              duration: 3000
            })
            return 0;
          }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.login({
    //   success:function(res){
    //     console.log(res)
    //   }
    // })
    this.getPintuan(1,0,10)
    var that=this
    wx.getSystemInfo({
      success: function (res) {
       
        that.setData({
          zheHeight: res.windowHeight
        })
      },
    })
    console.log(that.data.zheHeight)
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPintuan(1, 0, 10)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var pageNo=this.data.pageNo
    try{
      this.getPintuan(pageNo,(pageNo-1)*10,10)
      
    }catch(err){
      cosnole.log(err)
      wx.showToast({
        title: '暂时没有更多的数据~~~~',
        icon:'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})